<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );
include 'weixin/wechatOpenId.php';
include 'weixin/wx_http.php';

switch ($ts) {
	case 'onlogin':
		$_POST	=	json_decode(file_get_contents("php://input"),1);
		$code = $_POST[ 'code' ];
		$nickName = $_POST[ 'nickName' ];
		$picture = $_POST[ 'picture' ];


		$access_odj		=	 new wechatOpenId($GLOBALS['AppId'],$GLOBALS['Secret'],$code);
		$result			=	$access_odj->get();

		if(!$result)
		{
			echo tojson1( array() );exit;
		};
		$member_data	=	$new['home']->find('hairdresser','open_id = "'.$result['openid'].'"');
		if($member_data)
		{
			if($member_data['type_id']==0)
			{
				$result['hairdresser_id']	=	$member_data['hairdresser_id'];
				$result['ism']				=	-1;
			}else
			{

				$result['hairdresser_id']	=	$member_data['hairdresser_id'];
				$result['ism']				=	$member_data['ism'];
				if($member_data['stat']==1)
				{
					$result['hairdresser_id']	=	$member_data['hairdresser_id'];
					$result['ism']				=	-1;
				}
			}
			$new['home']->update('hairdresser','hairdresser_id='.$member_data['hairdresser_id'],array('name'=>$nickName,'	picture'=>$picture));

		}else{

			$hairdresser_id	=	$new['home']->create('hairdresser',array('name'=>$nickName,'	picture'=>$picture,'open_id'=>$result['openid'],'type_id'=>0,'stat'=>0,'level_id'=>0,'create_at'=>time()));
			$result['hairdresser_id']	=	$hairdresser_id;
			$result['ism']				=	-1;
		}
		echo tojson1( $result );exit;
		break;
	case 'serv_add':
		extract( $_POST );
		$data = array();
		$data[ 'item_name' ] = isset( $item_name ) ? h( $item_name ) : '';
		$data[ 'item_time' ] = $data[ 'create_at' ] = time();
		$lastId = $new[ 'home' ]->create('service_item',$data );
		if( $lastId ){
			echo '{"result":1,"message":"添加成功！"}'; 
		}else{
			echo '{"result":0,"message":"添加失败！"}'; 
		}
	break;
 
	case 'serv_get_data':
		$item_id = $_GET[ 'item_id' ]; 
		$itemInfo = $new[ 'home' ]->find('service_item',array( 'item_id' => $item_id ) );
		 
		echo tojson1( $itemInfo );exit;
		 
	break;
	case 'serv_get_all':
		$item_id = $_GET[ 'item_id' ]; 
		$itemList = $new[ 'home' ]->findAll('service_item');
		 
		echo tojson1( $itemList );exit;
	break;
	case 'serv_get_conf':	
		$confList = $new[ 'home' ]->findAll('set_conf');
		
		 
		echo tojson1( $confList );
	break;
	
	case 'orders_add':

		$_POST	=	json_decode(file_get_contents("php://input"),1);
		$data[ 'order_name' ] 		= 	$_POST['order_name'] ;
		$data[ 'price' ] 			= 	$_POST['order_price'] ;
		$data[ 'order_code' ] 		=	date('YmdHis',time()).rand(10,99).rand(10,99).rand(10,99).rand(10,99);
		$data[ 'order_status' ] 	= 	0;
		$data[ 'item_id' ] 		= 	$_POST['item_id'] ;
		$data[ 'hairdresser_id' ] = $_POST['hairdresser_id'] ;
		$data[ 'member_id' ] 		= $_POST['member_id'] ;
		$data[ 'subscribe_time' ] = $_POST['order_time'];
		$data[ 'remarks' ] = 	'' ;
		$data[ 'mphone' ] = 	$_POST['iphone'] ;
		$data[ 'sex' ] = 	$_POST['sex'] ;
		$data[ 'create_at' ] = 	time() ;
		$lastId = $new[ 'home' ]->create('orders', $data );
		if( $lastId ){
			echo '{"result":1,"message":"添加成功！"}'; 
		}else{
			echo '{"result":0,"message":"添加失败！"}'; 
		}
		
	break;
	case 'orders_get_data':
		$order_id = $_GET[ 'order_id' ]; 
		$orderInfo = $new[ 'home' ]->find('orders',array( 'order_id' => $order_id ) );
		
		 
		echo tojson1( $orderInfo );exit;
	break;
	case 'orders_get_all':
		$ism = $_GET[ 'ism' ];
		$hid = intval(  $_GET[ 'hid' ] );
		$orderStat = intval( $_GET[ 'orderStat' ] ) > 0 ? intval( $_GET[ 'orderStat' ] ) : 0 ;

		if( $hid > 0 ){
			$conStr .= 'member_id='.$hid;
		}
		if( $orderStat == 0 ){
			$conStr .= ' and order_status=0';
		}else{
			$conStr .= ' and order_status!=0';
		}
		//ECHO 'hid:'.$hid;

		$orderList = $new[ 'home' ]->findAll('orders',$conStr ,'subscribe_time desc');	 

		foreach( $orderList as $key => $val ){
			$orderList[ $key ][ 'subscribe_time' ] = date( 'm/d H:i',$val[ 'subscribe_time' ] );
		}	
		//print_r($orderList);
		echo tojson1( $orderList );exit;		
		
	break;
	case 'memrber_orders':
		$_POST	=	json_decode(file_get_contents("php://input"),1);
		$orderList = $new[ 'home' ]->findAll('orders','member_id='.$_POST['member_id'].' and st');
		foreach( $orderList as $key => $val ){
			$orderList[ $key ][ 'subscribe_time' ] = date( 'Y-m-d H:i:s',$val[ 'subscribe_time' ] );
		}
		//print_r($orderList);
		echo tojson1( $orderList );exit;

		break;
	case 'get_hairdresser_order':
			$_POST	=	json_decode(file_get_contents("php://input"),1);
			$hairdresser_id = $_POST[ 'hairdresser_id' ];
			$item_id = $_POST['item_id'];
			$data	=	array();
			if(!$hairdresser_id || !$item_id)
			{
				echo tojson1( $data );exit;
			}

			//获取设计师及服务项
			$hairdresser_data	=	$new['home']->find('hairdresser','hairdresser_id='.$hairdresser_id);
			if($hairdresser_data)
			{
				$data['hairdresser']	=	$hairdresser_data;
			}else
			{
				$data['hairdresser']	=	'';
			}
			$service_item_data	=	$new['home']->find('service_item','item_id='.$item_id);
			$data['server_item']['item_id']		=	'';
			$data['server_item']['price']		=	'';
			$data['server_item']['subscribe']	=	'';
			$data['server_item']['item_time']	=	'';
			$data['server_item']['item_name']	=	'';
			$data['server_item']['level']	=	'';
			if($service_item_data)
			{
				$item_hairdresser_data	=	$new['home']->find('item_hairdresser_relation','hairdresser_id = '.$hairdresser_id.' and item_id='.$item_id);
				if($item_hairdresser_data)
				{
					$level_data	=	$new['home']->find('levels','level_id = '.$hairdresser_data['level_id']);
					if($level_data)
					{
						$data['server_item']['level']	=	$level_data['name'];
					}
					$data['server_item']['item_id']		=	$service_item_data['item_id'];
					$data['server_item']['price']			=	$item_hairdresser_data['price'];
					$data['server_item']['subscribe']	=	$item_hairdresser_data['subscribe'];
					$data['server_item']['item_time']	=	$service_item_data['item_time'];
					$data['server_item']['item_name']	=	$service_item_data['item_name'];
				}
			}





            /**
              * 一天开始时间1497661200 2017/6/17 09:00:00   2017/6/17 00:00:00  1497628800
              * 一天结束时间1497709800 2017/6/17 22:30:00   2017/6/17 00:00:00  1497628800
              * 当前时间：1497688655  2017/6/17 16:37:35
              * 1800/miao
             *
             * 1497688655 - 1497661200 = x
             * x/1800 = y
             */

			//开始时间

			$start_conf_data 	=	$new[ 'home' ]->find('set_conf',"column_name='start_time'" );

			if(!$start_conf_data)
			{
				$start_time = '09:00';
			}
			$start_time	=	$start_conf_data['column_value'];

		   //结束时间
			$end_conf_data 		=	$new[ 'home' ]->find('set_conf',"column_name='end_time'" );

			if(!$end_conf_data)
			{

				$end_time = '22:30';
			}
			$end_time	=	$end_conf_data['column_value'];



			//当前时间
			$time = time();

			//今日
			$today	=	date('Y-m-d',time());
			//明日
			$tomorrow	= date("Y-m-d",strtotime("+1 day"));

			$today_order_start_time	=	strtotime($today.' '.$start_time);
			$today_order_end_time	=	strtotime($today.' '.$end_time);
			if($today_order_end_time<$today_order_start_time)
			{
				$today_order_end_time	=	strtotime($tomorrow.' '.$end_time);
			}
			$n	=	ceil(($time-$today_order_start_time)/1800);

			$work_time = array();
			for($i=1;$i<=(($today_order_end_time-$today_order_start_time)/1800+1);$i++)
			{
				$work_time[$i]	=	$today_order_start_time+($i-1)*1800;
			}
			$str_time	=	array();
			$_str_time	=	array();
		$tomorrow	= date("Y-m-d",strtotime("+1 day"));

		$tomorrow_order_start_time	=	strtotime($tomorrow.' '.$start_time);
		$tomorrow_order_end_time	=	strtotime($tomorrow.' '.$end_time);
			$today_order_data		=	$new[ 'home' ]->findAll('orders',"hairdresser_id=".$hairdresser_id."  and subscribe_time >=".$today_order_start_time." and subscribe_time <= ".$tomorrow_order_start_time." and order_status =1");

			if($today_order_data)
			{

				foreach($work_time as $k=>$te)
				{
					//当天


					if($n<$k && $today_order_end_time > $time)
					{

						foreach($today_order_data as $v)
						{

							if($v['subscribe_time'] == $te)
							{
								$str_time[$k] = array('is_use'=>0,'strTime'=>date('H:i',$te),'timestamp'=>$te);
							}else{
								$str_time[$k] = array('is_use'=>1,'strTime'=>date('H:i',$te),'timestamp'=>$te);
							}
						}

					}else{

						$str_time[$k] = array('is_use'=>0,'strTime'=>date('H:i',$te),'timestamp'=>$te);

					}

				}

			}else
			{

				foreach($work_time as $k=>$te)
				{
					//当天

					if($n<$k && $today_order_end_time > $time)
					{
						$str_time[$k] = array('is_use'=>1,'strTime'=>date('H:i',$te) ,'timestamp'=>$te);
					}else{

						$str_time[$k] = array('is_use'=>0,'strTime'=>date('H:i',$te) ,'timestamp'=>$te);
					}

				}

			}

		$tomorrow	= date("Y-m-d",strtotime("+1 day"));

		$tomorrow_order_start_time	=	strtotime($tomorrow.' '.$start_time);
		$tomorrow_order_end_time	=	strtotime($tomorrow.' '.$end_time);
		$today_order_data  = array();
		$today_order_data		=	$new[ 'home' ]->findAll('orders',"hairdresser_id=".$hairdresser_id."  and subscribe_time >=".$tomorrow_order_start_time."  and order_status =1");
		if($today_order_data)
		{
			foreach($work_time as $k=>$te)
			{
				foreach($today_order_data as $v)
				{

					if($v['subscribe_time'] != $te)
					{
						$_str_time[$k] = array('is_use'=>1,'strTime'=>date('H:i',$te),'timestamp'=>$te);
					}else
					{
						$_str_time[$k] = array('is_use'=>0,'strTime'=>date('H:i',$te) ,'timestamp'=>$te);
					}
				}

			}

		}else
		{
			foreach($work_time as $k=>$te)
			{
				//当天
				$_str_time[$k] = array('is_use'=>1,'strTime'=>date('H:i',$te) ,'timestamp'=>$te);

			}

		}



		$data['today']		=	date('m月d日',time());
			//明日
		$data['tomorrow']	= date("m月d日",strtotime("+1 day"));
		$data['today_time']	=	$str_time;
		$data['tomorrow_time']	=	$_str_time;
		echo tojson1( $data );exit;

	break;
	case 'order_list' :
		$type = $_GET[ 'type' ];//0,1,2 待处理，已结算，已取消【没来消费】
		$orderList = array();
		$orderList = $new[ 'home' ]->findAll('orders','order_status='.$type );
		foreach( $orderInfo as $key=> $val ){
			$out[ $key ][ 'subscribe_time' ] = date('Y-m-d H:i:s',$val[ 'subscribe_time' ] );
			$out[ $key ][ 'price' ] = $val[ 'price' ];
			$out[ $key ][ 'order_name' ] = $val[ 'order_name' ];
			$out[ $key ][ 'hairdresser_id' ] = $val[ 'hairdresser_id' ];
		} 
		echo tojson1( $out );
		
	break;
	//手机验证码
	case 'hairdresser_add' :
		extract( $_POST );
		$data = array();
		$itemData = array();		
		$data[ 'name' ] = $name;
		$data[ 'level_id' ] = $level_id;
		$data[ 'picture' ] = $picture;
		$data[ 'create_at' ] = time();		
		$hairdresser_id = $new[ 'home' ]->create('hairdresser',$data );
		$itemData[ 'hairdresser_id' ] = $hairdresser_id;
		$itemData[ 'item_id' ] = $item_id;
		$itemData[ 'price' ] = $price;
		$itemData[ 'subscribe' ] = $subscribe;
		$itemData[ 'create_at' ] = time();
		$itemData[ 'update_at' ] = time();		
		
		if( $hairdresser_id ){
			$item_id = $new[ 'home' ]->create('item_hairdresser_relation',$itemData );
			if( !$item_id ){
				$new[ 'home' ]->delete('hairdresser','hairdresser_id='.$hairdresser_id );
				echo '{"result":0,"message":"插入失败！"}';
				exit;
			}
		}
		echo '{"result":1,"message":"操作成功！"}';
		exit;
		
	break;
	/**
     * 获取单个美发师记录
     * @param $hairdresser_id
     */
	case 'hairdresser_get_data' : 
		$hairdresser_id = $_POST[ 'itemId' ];
        $data = array();
        if( $hairdresser_id>0 ){
            $hairdresser_one = $new[ 'home' ]->find('hairdresser','hairdresser_id='.$hairdresser_id);
            if( $hairdresser_one ){
                $hairdresserItem_one = $new[ 'home' ]->find('item_hairdresser_relation' ,'hairdresser_id='.$hairdresser_id );
                if( $hairdresserItem_one )
                {
                    $data['hairdresser']    =   $hairdresser_one;
                    $data['hairdresser_item']    =   $hairdresserItem_one;
                    //return response()->json($data,200);
					echo tojson1($data);exit;
					 
                }

            }
        }
        echo '{"result":0,"message":"无数据！"}';
     
	break;
	case 'hairdresser_get_all':
		$data               =   array();
        $hairdresser_ids    =   array();
        $level_ids          =   array();

        $hairdressers       =   $new[ 'home' ]->findAll('hairdresser');
        if($hairdressers)
        {
            foreach($hairdressers as $v)
            {
                $hairdresser_ids[ $v[ 'hairdresser_id' ] ]    =   $v[ 'hairdresser_id' ];
                $level_ids[ $v[ 'hairdresser_id' ] ]          =   $v[ 'level_id' ];
            }
		/*	$hairStr = '';
			$hcount = count( $hairdresser_ids );
			$hh = 0;
			foreach( $hairdresser_ids as $key => $val ){
				if( $hcount == $hh ){
					$hairStr .= $val;
				}else{
					$hairStr .= $val.',';
				}
				$hh ++ ;
			}
			$levStr = '';
			$lcount = count( $level_ids );
			$ll = 0;
			
			foreach( $level_ids as $key => $val ){
				if( $lcount == $ll ){
					$levStr .= $val;
				}else{
					$levStr .= $val.',';
				}
				$ll ++ ;
			}*/
			$hairdresserItems = array();
			$levels = array();
			if($hairdresser_ids && $level_ids){
				$hairdresserItems   =   $new[ 'home' ]->findAll('item_hairdresser_relation','hairdresser_id in('.implode(',',$hairdresser_ids).')' );
				$levels             =   $new[ 'home' ]->findAll('levels','level_id in('.implode(',',$level_ids).')' );

            /*$hairdresserItems   =   $new[ 'home' ]->findAll('item_hairdresser_relation','hairdresser_id in('.$hairStr.')' );//HairdresserItem::all()->whereIn('hairdresser_id',$hairdresser_ids);
            $levels             =   $new[ 'home' ]->findAll('levels','level_id in('.$levStr.')' );*///Levels::all()->whereIn('level_id',$level_ids);
            if($hairdresserItems)
            {
                foreach($hairdresserItems as $v)
                {
                    $data[ 'hairdresserItems' ][ $v[ 'hairdresser_id' ] ]   =   $v;
                }
            }

            if($levels)
            {
                foreach($levels as $v)
                {
                    $data['levels'][ $v[ 'level_id' ] ]   =   $v;
                }
            }
			}
            $data['hairdressers']   =   $hairdressers;
			echo tojson1( $data );exit;
            //return response()->json($data,200);

        }
        echo '{"result":0,"message":"无数据！"}';
	break;
	case 'hairdresser_get_item_all':
		$item_id           =    $_GET[ 'item_id' ];
        $hairdresser_ids   =    array();
        $data              =    array();
        $levels             =   $new[ 'home' ]->findAll('levels');//Levels::all();

		//print_r($levels);
        $level_hairdresser  =   array();
        $hairdresser_item   =   array();
        $level_data         =   array();
        if($levels)
        {
            $data['levels']   =  $levels;
            if($item_id>0)
            {
                $hairdresserItems   =   $new[ 'home' ]->findAll('item_hairdresser_relation','item_id='.$item_id);//HairdresserItem::all()->where('item_id',$item_id);
                
				if($hairdresserItems)
                {
                    foreach($hairdresserItems as $v)
                    {
                        $hairdresser_ids[ $v[ 'hairdresser_id' ] ]    =   $v[ 'hairdresser_id' ];
                        $hairdresser_item[ $v[ 'hairdresser_id' ] ]    =   $v;
                    }

				
                    if($hairdresser_ids)
                    {
                        $hairdressers   =   $new[ 'home' ]->findAll('hairdresser','hairdresser_id in('.implode(',',$hairdresser_ids).') and type_id = 1 and ism = 0' );

						//Hairdresser::all()->whereIn('hairdresser_id',$hairdresser_ids);

						if($hairdressers)
                        {
                            foreach($hairdressers as $k=>$v)
                            {

                                if(isset($hairdresser_item[$v[ 'hairdresser_id' ] ]) && !empty($hairdresser_item[$v[ 'hairdresser_id' ] ] ) )
                                {
                                    $level_hairdresser[$v[ 'level_id']][ $k ]    = $v;
                                    $level_hairdresser[$v[ 'level_id']][$k]['item_id']    = $hairdresser_item[$v[ 'hairdresser_id' ]]['item_id'];
                                    $level_hairdresser[$v[ 'level_id']][$k]['price']    = $hairdresser_item[$v[ 'hairdresser_id' ]]['price'];
                                    $level_hairdresser[$v[ 'level_id']][$k]['subscribe']    = $hairdresser_item[$v[ 'hairdresser_id' ]]['subscribe'];
                                    $level_hairdresser[$v[ 'level_id']][$k]['subscribe']    = $hairdresser_item[$v[ 'hairdresser_id' ]]['subscribe'];
                                }

                            }

                        }
                    }
                    foreach($levels as $k=>$v)
                    {
                        $level_data[$k]['level_id']     =   $v[ 'level_id'];
                        $level_data[$k]['level_name']   =   $v['name'];
                        if(isset($level_hairdresser[$v['level_id']]) && !empty($level_hairdresser[ $v['level_id'] ]))
                        {
                            $level_data[$k]['hairdresser']   =   $level_hairdresser[ $v['level_id'] ];
                            $level_data[$k]['is_hairdresser']  =   true;
                        }else
                        {
                            $level_data[$k]['is_hairdresser']  =   false;
                        }

                    }
                    $data['hairdresserItems']   =  $hairdresserItems;
                }




				}
		}
			// print_r($level_data);
			echo tojson1($level_data); exit;
		 
	break;
	case "setting_all":
		$conf_data     =   $new[ 'home' ]->findAll('set_conf');//Levels::all();
		$data		   =	array();

		if($conf_data)
		{
			foreach($conf_data as $v)
			{
				$data[$v['column_name']]  =   $v['column_value'];
				if($v['column_name']=='store_banner'){
					$data['store_banner']	=	$GLOBALS['_api_url'].$v['column_value'];
				}
				if($v['column_name']=='store_qr_code'){
					$data['store_qr_code']	=	$GLOBALS['_api_url'].$v['column_value'];
				}
			}

		}

		echo tojson1($data); exit;
	case "order_hairdresser":
		date_default_timezone_set('Asia/Shanghai');
		$hairdresser_id	=	$_POST['hairdresser_id'];
		$hairdresser_id = 1;
		$use_time = time();
		/**
		 * 一天开始时间1497661200 2017/6/17 09:00:00   2017/6/17 00:00:00  1497628800
		 * 一天结束时间1497709800 2017/6/17 22:30:00   2017/6/17 00:00:00  1497628800
		 * 当前时间：1497688655  2017/6/17 16:37:35
		 * 1800/miao
		 *
		 * 1497688655 - 1497661200 = x
		 * x/1800 = y
		 *
		 * z= 1800
		 * n = 28
		 *
		 * a=['09:30']
		 * ....
		 * a=['22:30']
		 *
		 *
		 * [1497661200]       =
		 * [1497661200+(n*z)] =
		 * [1497709800]       =
		 *
		 * $
		 *
		 *
		 *
		 *
		 */


	case 'member_add':
		extract( $_POST );
		$data = array();
		$data[ 'username' ] = $username;
		$data[ 'alias' ] = $alias;
		$data[ 'open_id' ] = $open_id;
		$data[ 'picture' ] = $picture;
		$data[ 'level' ] = $level;
		$data[ 'create_at' ] = time();
		$lastId = $new[ 'home' ]->create('members',$data);
		 
            if($lastId)
            {
               echo '{"result":1,"message":"添加成功！"}';exit;
            };

            echo '{"result":0,"message":"添加失败！"}';
	
	break;
	case 'member_get_data':
		$member_id = $_GET[ 'member_id' ];
		$members =   $new[ 'home' ]->find('members','member_id='.$member_id);//Members::find($member_id);
        if($members)
        {
			 
			echo tojson1($members);exit;
            //return response()->json($members,200);
        }else
        {
             echo '{"result":0,"message":"参数错误！"}';//return response()->json((object)array(),200);
        }
	break;
	case 'leves_add':
		extract( $_POST );
		$data = array();
		$data[ 'name' ] = $name;
		$data[ 'create_at' ] = time();
		$lastId = $new[ 'home' ]->create('levels',$data);
		  if($lastId)
            {
               echo '{"result":1,"message":"添加成功！"}';exit;
            };
            echo '{"result":0,"message":"添加失败！"}';
	break;
	case 'leves_get_data':
		$level_id = $_GET[ 'level_id' ];
		$levels =   $new[ 'home' ]->find('levels','level_id='.$level_id);//Levels::find($level_id);
        if($levels)
        {
             
			echo tojson1($levels);exit;
        }else{
            echo '{"result":0,"message":"参数错误！"}';//return response()->json((object)array(),200);
        }
	break;
	case 'leves_get_all':
		$levels =   $new[ 'home' ]->findAll('levels');//Levels::All();
        if($levels)
        {
			echo tojson1($levels);exit;
        }else{
            echo '{"result":0,"message":"参数错误！"}';//return response()->json((object)array(),200);
        }
	break;
	case 'leves_get_all_arr':

		$levels =   $new[ 'home' ]->findAll('levels');//Levels::All();

		$data   = 	array();
		if($levels)
		{
			foreach($levels as $v)
			{
				$data[$v['level_id']]	=	$v['name'];
			}
			echo tojson1($data);exit;
		}else{
			echo '{"result":0,"message":"参数错误！"}';//return response()->json((object)array(),200);
		}
		break;


















}